/**
 * Created by zmliu on 2014/8/1.
 */
function (require, exports, module) {
    var jq = require("jquery");
    var Tooltip = function (options) {
        this.options = {
            target: jq.noop(),
            message: "提示信息",
            zIndex: 99999,
            errorClassName: "input_error",
            addErrorClass: true,
            container : "body"
        }
        if (options) {
            jq.extend(this.options, options)
        }
        this.defaults = {
            templete: jq('<div class="base_alert" style="max-width:250px;z-index:' + this.options.zIndex + ';white-space:nowrap;"><b style="top:50%;margin-top:-6px;"></b><i style="top:50%;margin-top:-6px;"></i><div class="alert_info"><span class="prompt_ico"></span></div></div>')
        }
        this.init();
    }
    Tooltip.prototype = {
        consotructor: Tooltip,
        // 初始化
        init: function () {
            this._createTooltip();
            this._event();
        },
        _event: function () {
            var self = this;
            jq(this.options.target).focus(function () {
                self.remove.call(self);
            });
            jq(document).click(function () {
                setTimeout(function () {
                    jq(self.options.target).removeAttr("error");
                }, 0)
                self.remove.call(self);
            });
        },
        // 输出节点位置信息
        _getNodePostion: function (customTarget) {
            var opts = this.options;
            // var customTarget = $(opts.target).attr("tooltip");
            var customTarget = jq(opts.target).attr("tooltip");
            var node = customTarget ? customTarget : opts.target;
            if (customTarget) {
                jq(node).removeClass(opts.errorClassName);
            }
            var width = jq(node).outerWidth();
            var target = {
                width: jq(node).outerWidth(),
                height: jq(node).outerHeight(),
                left: jq(node).offset().left,
                top: jq(node).offset().top
            }
            var tooltip = {
                width: this.defaults.templete.outerWidth(),
                height: this.defaults.templete.outerHeight()
            }
            var top = target.top + (target.height - tooltip.height) / 2
            return {
                left: Math.round(target.left + target.width + 7),
                top: Math.round(top)
            }
        },
        // 生成tooltip
        _createTooltip: function () {
            var opts = this.options;
            jq(".base_alert").remove();
            jq(opts.container).append(this.defaults.templete.find(".alert_info").append(opts.message).end());
            var position = this._getNodePostion();
            if (opts.addErrorClass) {
                jq(opts.target).addClass(opts.errorClassName);
            }
            jq(opts.target).attr("error", true);
            this.defaults.templete.css(position);
        },
        // 删除tooltip
        remove: function () {
            if (jq(this.options.target).attr("error")) {
                return;
            }
            this.defaults.templete.remove();
        }
    };
    module.exports = Tooltip;
}